<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:um="livcos.org/ns/mgt/user"
  xmlns="http://www.w3.org/1999/XSL/Format"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="doc pm um f xs">
	
	<xsl:import href="news.xsl"/>
	
	<xsl:variable name="task-bg-colors" select="('#FFBBAA','#FFCCBB','#FFDF90','#FCF088','#EEE7BB')"/>
	<xsl:variable name="label-colors" select="('#FFFFCC','#000000','#000000','#000000','#000000')"/>
	<xsl:variable name="label-bg-colors" select="('#FF3D3D','#FF8A6A','#FFBB33','#FFE700','#DDD6AA')"/>
	<xsl:variable name="completed-bg-colors" select="('#FBEDEE','#FBECEB','#F9EFDA','#fcf9d6','#F8F8DD')"/>
	
	<xsl:attribute-set name="task-style">
		<xsl:attribute name="background-color"></xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:template match="pm:task">
		<xsl:variable name="p" select="if (@priority) then @priority else 3" as="xs:integer"/>
		<xsl:variable name="progress"
		select="if (not(@progress) and (@effort > 0)) then (@effort div @estimate) * 100 else @progress"/>
		<block xsl:use-attribute-sets="block" keep-together="always"
			background-color="{if ($progress &gt;= 100) then $completed-bg-colors[$p] else $task-bg-colors[$p]}">
			<block text-align="right" font-size="1pt" margin-top="0pt" margin-right="1pt" margin-bottom="0pt" padding-top="1pt">
				<xsl:choose>
					<xsl:when test="$progress &gt; 0">
						<inline border="solid" border-width="thin" border-color="gray">
							<inline background-color="blue">
								<xsl:for-each select="1 to $progress">&#160;</xsl:for-each>
							</inline>
							<inline>
								<xsl:for-each select="$progress to 100">&#160;</xsl:for-each>
							</inline>
						</inline>
					</xsl:when>
					<xsl:when test="$progress">
						<inline border="solid" border-width="thin" border-color="gray">
							<xsl:for-each select="1 to 100">&#160;</xsl:for-each>
						</inline>
					</xsl:when>
				</xsl:choose>
			</block>
			<block margin-top="-2pt" margin-right="0pt" margin-bottom="0pt" padding-bottom="0pt">
				<xsl:choose>
					<xsl:when test="$progress &gt;= 100">
						<inline font-size="smaller">TASK:</inline>
					</xsl:when>
					<xsl:otherwise>
						<inline font-size="smaller" color="{$label-colors[$p]}"
							background-color="{$label-bg-colors[$p]}" padding-top="2pt" padding-bottom="1pt">
							<xsl:value-of select="if ($p = 1) then 'TODO!' else 'TODO:'"/>
						</inline>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> </xsl:text>
				<xsl:apply-templates select="pm:title/node()"/>
			</block>
		</block>
		<xsl:apply-templates select="pm:description | doc:*"/>
		<xsl:if test="pm:depends">
			<block xsl:use-attribute-sets="block">Depends on <xsl:apply-templates select="pm:depends"/>.</block>
		</xsl:if>
		<xsl:if test="pm:source">
			<block xsl:use-attribute-sets="block">Depends on <xsl:apply-templates select="pm:source"/>.</block>
		</xsl:if>
	</xsl:template>

	<xsl:template match="pm:description">
		<block xsl:use-attribute-sets="block">
			<xsl:apply-templates select="node()"/>
		</block>
	</xsl:template>
	
	<xsl:template match="pm:depends">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:if test="doc:*">
			<xsl:attribute name="title" select="doc:*[1]"/>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="not(@on) or (@on = '') or (@on = '#')">-- task --</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="f:entity(@on, .)" mode="list"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="pm:source">
		<xsl:if test="position() > 1">, </xsl:if>
		<xsl:if test="doc:*">
			<xsl:attribute name="title" select="doc:*[1]"/>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="not(@ref) or (@ref = '') or (@ref = '#')">-- source --</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="f:entity(@ref, .)" mode="list"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template match="pm:task" mode="list">
		<xsl:value-of select="pm:title"/>
	</xsl:template>
	
	<xsl:template match="um:person" mode="list">
		<xsl:value-of select="um:name"/>
		<xsl:text> </xsl:text>
		<xsl:value-of select="um:first-name"/>
	</xsl:template>
	
</xsl:stylesheet>
